package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.domain.DishFlavor;
import com.itheima.dto.DishDto;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 菜品下拉列表
     * @param categoryId
     * @return
     */

    @GetMapping("/list")
    public R<List<DishDto>> dishPulldown(String categoryId){
        log.info("------下拉分类----");
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,categoryId);
        List<Dish> dishes = dishService.list(queryWrapper);
        //新建一个Dto类 并将查询结果传入;
        List<DishDto> dishDtos = new ArrayList<>();
        for (int i = 0; i < dishes.size(); i++) {
            Dish dish = dishes.get(i);
            //找到当前dish对应的id
            LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper1);
            DishDto dishDto = new DishDto();
            //复制到新的dto类
            BeanUtils.copyProperties(dish,dishDto);
      //      BeanUtils.copyProperties(dishFlavorList,dishDto.getFlavors());
            dishDto.setFlavors(dishFlavorList);
            dishDtos.add(dishDto);
        }
        return R.success(dishDtos);
    }

    /**
     * 新增菜品
     * @param dishdto
     * @return
     */
    // api 这里根据接口文档返回的应该是个String 但是 改成一个DishDto后
    // apipost 里新增的数据显示口味表全是null,没有自动填充,也没有把操作者id填进去
    //最后解决方案是 把dto拆成两段 分两次存入两个数据表里
    @PostMapping
    public R<DishDto> save(@RequestBody DishDto dishdto){
        log.info("---新增菜品----");

        LambdaQueryWrapper<Dish> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getName,dishdto.getName());
        Dish d1 = dishService.getOne(queryWrapper);

        if(d1!=null){
            log.info("您添加的菜品已存在");
            return  R.error("您添加的菜品名称已存在");
        }
        DishDto dishdtoPlus = dishService.fillDish(dishdto);
//        dishService.save(dishdtoPlus);
        return R.success(dishdtoPlus);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> selectById(@PathVariable String id){
        DishDto dishDto = dishService.getdishDto(id);
        return  R.success(dishDto);
    }


    /**
     * 菜品的分页查找
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> selectPage(Integer page, Integer pageSize, String name) {
        List<DishDto> dtoList = dishService.selectPage(page, pageSize, name);
//        //查询总记录数
//        long dishCount= dishService.getCountByName(name);

        //组装数据
        Page<DishDto> dtoPage = new Page<>();

        dtoPage.setSize(pageSize);
        dtoPage.setCurrent(page);
        dtoPage.setRecords(dtoList);


        return R.success(dtoPage);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info("修改菜品信息:{}",dishDto);

        dishService.update(dishDto);
        return R.success("修改菜品成功");
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids){
        log.info("------删除菜品:{}-------",ids);
        dishService.delete(ids);
        return R.success("删除成功");
    }

    /**
     * 启用禁用菜品
     * @param ids
     * @return
     */
    @PostMapping ("/status")
    public R<String> updateState(String ids){
        log.info("---批量修改菜品状态:{}----",ids);
        dishService.updateState(ids);
        return  R.success("状态修改成功");
    }
}
